package com.foreknow.demo2;

public class ArrayDemo2 {
    public static void main(String[] args) {
        String[] s1 = new String[3];
        s1[0] = "fender";
        s1[1] = "gibson";
//        s1[2] = "yamaha";

        //如何遍历数组 s1.length表示数组中元素的个数
        for(int i = 0;i < s1.length;i++){
//            String x = s1[i];
            System.out.println(s1[i]);
        }
        System.out.println("------------------增强for循环----------------------");
        for (String x:s1) {
            System.out.println(x);
        }
        System.out.println("-----------------------练习---------------------------");
        //定义一个整型数组，计算数组中所有元素之和
        //1. 定义一个数组
        int[] array = new int[3];
        array[0] = 1;
        array[1] = 2;
        array[2] = 3;
        //2. 遍历数组累加求和
        int sum = 0;
        for (int i = 0;i<array.length;i++){
//            int x = array[i];
            sum += array[i];
        }
        System.out.println(sum);




    }
}
